import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import vuexPre from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list:[1,2,3,4,5],
    number:10,
    str:'hello Vuex',
    resList:[],//存放任务
  },
  getters: {
    getTotal(state){
        let total = 0
        state.list.forEach(item=>{
            total+=item

        })
        return total

    },
    sum(){
        return '123'
    },
    // 筛选正在进行的任务
    doing(state){
        return state.resList.filter(item=>!item.status)
    },
    // 筛选已经完成的任务
    done(state){
        return state.resList.filter(item=>item.status)
    }

  },
  mutations: {
    del(state,index){
       state.list.splice(index,1)
    },
    add(state,title){
        state.list.push(title)
    },
    resAdd(state,data){
        state.resList = data

    },
    addOk(state,title){
      let flag=  state.resList.some(item=>item.title == title)
      if(flag){
        alert('重复')
        return
      }
       state.resList.push({
          id:new Date().getTime(),
          title,
          status:false
       })
    },
    // 删除
    delOk(state,id){
        state.resList.forEach((item,index)=>{
            if(item.id == id){
                state.resList.splice(index,1)
            }
        })

    },
    // 更改状态
    changeStatus(state,obj){
        let {index,ck} = obj
        state.resList[index].status = ck

    }

  },
  actions: {
    getList({commit}){
       axios.get('./list.json').then(res=>{
        console.log(res);
          commit('resAdd',res.data.list)
       })

    }
  },
  modules: {
  },
  plugins:[vuexPre()]
})
